package com.morehair.service.wx.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.morehair.bean.bo.wx.OrderCommentBo;
import com.morehair.bean.bo.wx.OrderSubmitBo;
import com.morehair.bean.bo.wx.WxOrderCancelBo;
import com.morehair.bean.pojo.*;
import com.morehair.bean.vo.wx.*;
import com.morehair.config.MallConfig;
import com.morehair.mapper.*;
import com.morehair.service.wx.WxOrderService;
import com.morehair.utils.GetNowUser;
import com.morehair.utils.OrderStatusUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author raohui
 * @Date 2021/6/9 19:35
 */

@Service
@Transactional
public class WxOrderServiceImpl implements WxOrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    CartMapper cartMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponUserMapper couponUserMapper;
    @Autowired
    ProductOfGoodsMapper goodsProductMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    MallConfig mallConfig;

    @Override
    public Integer submitOrder(OrderSubmitBo orderInfo) {
        Order order = new Order();
        Integer userId = GetNowUser.getUser().getId();
        if (userId != null) {
            if (!orderInfo.getCartId().equals(0)) {
                //不等于0，表示只有单个物品下单，只需要处理这个购物车内容就行
                Cart cart = cartMapper.selectByPrimaryKey(orderInfo.getCartId());
                Address address = addressMapper.selectByPrimaryKey(orderInfo.getAddressId());

                order.setGoodsPrice(cart.getPrice().multiply(BigDecimal.valueOf(cart.getNumber().intValue())));
                order.setFreightPrice(BigDecimal.valueOf((cart.getPrice().multiply(new BigDecimal(cart.getNumber())).doubleValue() >= mallConfig.getExpressFreightMin()) ? 0 : mallConfig.getExpressFreightValue()));

                setOrderInfo(orderInfo, order, userId, address);

                if (setOrderGoods(order, cart) == -1) {
                    try {
                        int i = 1 / 0;
                    } catch (Exception e) {
                        throw new ArithmeticException();
                    }
                }

                //需要评论的数目
                Order cskaoyanmallOrder1 = new Order();
                cskaoyanmallOrder1.setId(order.getId());
                cskaoyanmallOrder1.setComments(cart.getNumber());
                orderMapper.updateByPrimaryKeySelective(cskaoyanmallOrder1);

                //插入order表一行数据，插入order_product表一行数据，修改good_product库存为减去order_goods的num
            } else {
                //等于0，表示可能有多个物品下单，查询所有checked等于1
                Address cskaoyanmallAddress = addressMapper.selectByPrimaryKey(orderInfo.getAddressId());//地址信息
                CartExample cskaoyanmallCartExample = new CartExample();
                CartExample.Criteria criteria = cskaoyanmallCartExample.createCriteria();
                criteria.andDeletedEqualTo(false);
                criteria.andCheckedEqualTo(true);
                criteria.andUserIdEqualTo(userId);
                List<Cart> cskaoyanmallCarts = cartMapper.selectByExample(cskaoyanmallCartExample);//拿到结账时所有的商品
                BigDecimal totalPrice = new BigDecimal(0);
                for (Cart cskaoyanmallCart : cskaoyanmallCarts) {
                    totalPrice = totalPrice.add(cskaoyanmallCart.getPrice().multiply(BigDecimal.valueOf(cskaoyanmallCart.getNumber().intValue())));
                }
                order.setGoodsPrice(totalPrice);
                order.setFreightPrice(BigDecimal.valueOf((totalPrice.doubleValue() >= mallConfig.getExpressFreightMin()) ? 0 : mallConfig.getExpressFreightValue()));

                setOrderInfo(orderInfo, order, userId, cskaoyanmallAddress);
                int commentsNum = cskaoyanmallCarts.size();
                for (Cart cskaoyanmallCart : cskaoyanmallCarts) {
//                    commentsNum = commentsNum + cskaoyanmallCart.getNumber();
                    if (setOrderGoods(order, cskaoyanmallCart) == -1) {
                        try {
                            int i = 1 / 0;
                        } catch (Exception e) {
                            throw new ArithmeticException();
                        }
                    }
                }
                //需要评论的数目
                Order cskaoyanmallOrder1 = new Order();
                cskaoyanmallOrder1.setId(order.getId());
                cskaoyanmallOrder1.setComments((short) commentsNum);
                orderMapper.updateByPrimaryKeySelective(cskaoyanmallOrder1);

                //插入order表一行数据，插入order_product表一行数据，修改good_product库存
            }
            return order.getId();
        }
        return null;
    }



    @Override
    public WxOrderInfoListVo getOrderList(Integer showType, Integer page, Integer size, Integer userId) {

        if (showType == null) {
            return null;
        }
        PageHelper.startPage(page, size);

        WxOrderInfoListVo wxOrderInfoListVo = new WxOrderInfoListVo();

        OrderExample cskaoyanmallOrderExample = new OrderExample();
        cskaoyanmallOrderExample.setOrderByClause("update_time desc");
        OrderExample.Criteria criteria = cskaoyanmallOrderExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId == null) {
            return null;
        }
        criteria.andUserIdEqualTo(userId);
        if (!showType.equals(0)) {
            if (showType.equals(1)) {
                checkEndTime(userId, cskaoyanmallOrderExample);
                criteria.andOrderStatusEqualTo(101);
            }
            if (showType.equals(2)) {
                criteria.andOrderStatusEqualTo( 201);
            }
            if (showType.equals(3)) {
                criteria.andOrderStatusEqualTo(301);
            }
            if (showType.equals(4)) {
                ArrayList<Short> shorts = new ArrayList<>();
                shorts.add((short) 401);
                shorts.add((short) 402);
                criteria.andOrderStatusIn(shorts);
            }
        } else {
            checkEndTime(userId, cskaoyanmallOrderExample);
        }

        List<Order> cskaoyanmallOrders = orderMapper.selectByExample(cskaoyanmallOrderExample);
        List<WxOrderListVo> wxOrderListVos = new ArrayList<>();
        for (Order cskaoyanmallOrder : cskaoyanmallOrders) {
            WxOrderListVo wxOrderListVo = new WxOrderListVo();
            wxOrderListVo.setId(cskaoyanmallOrder.getId());
            wxOrderListVo.setOrderStatusText(OrderStatusUtil.getOrderStatus(cskaoyanmallOrder.getOrderStatus()));
            wxOrderListVo.setIsGroupin(false);//团购内容
            wxOrderListVo.setOrderSn(Long.parseLong(cskaoyanmallOrder.getOrderSn()));
            wxOrderListVo.setActualPrice(new BigDecimal(String.valueOf(cskaoyanmallOrder.getActualPrice())).doubleValue());

            List<WxOrderGoodListVo> goodsList = orderMapper.getOrderGoodsList(cskaoyanmallOrder.getId());
            wxOrderListVo.setGoodsList(goodsList);
            if (cskaoyanmallOrder.getComments().equals((short) 0)) {
                wxOrderListVo.setHandleOption(OrderStatusUtil.getHandleOption(cskaoyanmallOrder.getOrderStatus()));
            }
            wxOrderListVos.add(wxOrderListVo);
        }

        wxOrderInfoListVo.setData(wxOrderListVos);
        wxOrderInfoListVo.setCount((int) new PageInfo<>(cskaoyanmallOrders).getTotal());
        wxOrderInfoListVo.setTotalPages(new PageInfo<>(cskaoyanmallOrders).getPages());

        return wxOrderInfoListVo;
    }

    @Override
    public WxOrderDetailVo getOrderDetail(Integer orderId) {
        WxOrderDetailVo wxOrderDetailVo = new WxOrderDetailVo();

        Order cskaoyanmallOrder = orderMapper.selectByPrimaryKey(orderId);
        if (cskaoyanmallOrder != null) {
            WxOrderDetailInfoVo wxOrderDetailInfoVo = new WxOrderDetailInfoVo(cskaoyanmallOrder.getId(), cskaoyanmallOrder.getConsignee(), cskaoyanmallOrder.getAddress(), cskaoyanmallOrder.getAddTime(), cskaoyanmallOrder.getOrderSn(), cskaoyanmallOrder.getActualPrice(), cskaoyanmallOrder.getMobile(), OrderStatusUtil.getOrderStatus(cskaoyanmallOrder.getOrderStatus()), cskaoyanmallOrder.getGoodsPrice(), cskaoyanmallOrder.getCouponPrice(), cskaoyanmallOrder.getFreightPrice(), OrderStatusUtil.getHandleOption(cskaoyanmallOrder.getOrderStatus()));
//            if (cskaoyanmallOrder.getComments().intValue() > 0) {
//                wxOrderDetailInfoVo.setHandleOption(OrderStatusUtil.getHandleOption((short) 443));
//            }
            wxOrderDetailVo.setOrderInfo(wxOrderDetailInfoVo);
            OrderGoodsExample cskaoyanmallOrderGoodsExample = new OrderGoodsExample();
            OrderGoodsExample.Criteria criteria = cskaoyanmallOrderGoodsExample.createCriteria();
            criteria.andOrderIdEqualTo(cskaoyanmallOrder.getId());
            List<OrderGoods> cskaoyanmallOrderGoods = orderGoodsMapper.selectByExample(cskaoyanmallOrderGoodsExample);
            wxOrderDetailVo.setOrderGoods(cskaoyanmallOrderGoods);

            if (cskaoyanmallOrder.getOrderStatus().equals((short) 301)) {
                ExpressInfoVo expressInfoVo = new ExpressInfoVo();
                expressInfoVo.setShipperName(cskaoyanmallOrder.getShipChannel());
                expressInfoVo.setLogisticCode(cskaoyanmallOrder.getShipSn());
                wxOrderDetailVo.setExpressInfo(expressInfoVo);
            }
        }
        return wxOrderDetailVo;
    }

    @Override
    public void addComment(OrderCommentBo orderInfo) {
            OrderGoods cskaoyanmallOrderGoods = orderGoodsMapper.selectByPrimaryKey(orderInfo.getOrderGoodsId());
            Comment cskaoyanmallComment = new Comment();
            cskaoyanmallComment.setId(null);
            cskaoyanmallComment.setValueId(cskaoyanmallOrderGoods.getGoodsId());
            cskaoyanmallComment.setType((byte) 0);
            cskaoyanmallComment.setContent(orderInfo.getContent());
            cskaoyanmallComment.setUserId(GetNowUser.getUser().getId());
            cskaoyanmallComment.setHasPicture(orderInfo.getHasPicture());
            cskaoyanmallComment.setPicUrls(orderInfo.getPicUrls().toString());
            cskaoyanmallComment.setStar(orderInfo.getStar().shortValue());
            cskaoyanmallComment.setAddTime(new Date());
            cskaoyanmallComment.setUpdateTime(new Date());
            cskaoyanmallComment.setDeleted(false);
            commentMapper.insertSelective(cskaoyanmallComment);

//            CskaoyanmallOrderGoods cskaoyanmallOrderGoods1 = orderGoodsMapper.selectByPrimaryKey(orderInfo.getOrderGoodsId());
//            System.out.println(cskaoyanmallComment.getId());
            cskaoyanmallOrderGoods.setComment(cskaoyanmallComment.getId());
            orderGoodsMapper.updateByPrimaryKeySelective(cskaoyanmallOrderGoods);
            Order cskaoyanmallOrder = orderMapper.selectByPrimaryKey(cskaoyanmallOrderGoods.getOrderId());
            cskaoyanmallOrder.setComments((short) (cskaoyanmallOrder.getComments() - 1));
//            cskaoyanmallOrder.set
            orderMapper.updateByPrimaryKeySelective(cskaoyanmallOrder);
    }

    @Override
    public void cancelOrder(Integer orderId) {
        Order cskaoyanmallOrder = orderMapper.selectByPrimaryKey(orderId);
//        cskaoyanmallOrder.setDeleted(true);
        cskaoyanmallOrder.setOrderStatus((short) 102);
        cskaoyanmallOrder.setUpdateTime(new Date());
        cskaoyanmallOrder.setEndTime(null);
        orderMapper.updateByPrimaryKeySelective(cskaoyanmallOrder);

        //cancel后，把库存还回去
       OrderGoodsExample cskaoyanmallOrderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = cskaoyanmallOrderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(cskaoyanmallOrder.getId());
        List<OrderGoods> cskaoyanmallOrderGoods = orderGoodsMapper.selectByExample(cskaoyanmallOrderGoodsExample);
        for (OrderGoods cskaoyanmallOrderGood : cskaoyanmallOrderGoods) {
            //CskaoyanmallGoodsProduct cskaoyanmallGoodsProduct =
            ProductOfGoods cskaoyanmallGoodsProduct = goodsProductMapper.selectByPrimaryKey(cskaoyanmallOrderGood.getProductId());
            cskaoyanmallGoodsProduct.setNumber(cskaoyanmallGoodsProduct.getNumber() + cskaoyanmallOrderGood.getNumber());
//            cskaoyanmallOrderGood.setNumber();
            goodsProductMapper.updateByPrimaryKey(cskaoyanmallGoodsProduct);
        }

//        //把order_goods里面的东西设置为delete
//        CskaoyanmallOrderGoodsExample cskaoyanmallOrderGoodsExample = new CskaoyanmallOrderGoodsExample();
//        CskaoyanmallOrderGoodsExample.Criteria criteria = cskaoyanmallOrderGoodsExample.createCriteria();
//        criteria.andOrderIdEqualTo(orderId);
//        List<CskaoyanmallOrderGoods> cskaoyanmallOrderGoods = orderGoodsMapper.selectByExample(cskaoyanmallOrderGoodsExample);
//        for (CskaoyanmallOrderGoods cskaoyanmallOrderGood : cskaoyanmallOrderGoods) {
//            cskaoyanmallOrderGood.setDeleted(true);
//            cskaoyanmallOrderGood.setUpdateTime(new Date());
//            orderGoodsMapper.updateByPrimaryKeySelective(cskaoyanmallOrderGood);
//        }
    }

    @Override
    public void refundOrder(Integer orderId) {
        Order cskaoyanmallOrder = orderMapper.selectByPrimaryKey(orderId);
        cskaoyanmallOrder.setOrderStatus((short) 202);
        cskaoyanmallOrder.setUpdateTime(new Date());
        orderMapper.updateByPrimaryKeySelective(cskaoyanmallOrder);
    }

    @Override
    public void deleteOrder(WxOrderCancelBo orderId) {
        //删除订单，同时会把order和order_goods里面的东西删除
        Order cskaoyanmallOrder = orderMapper.selectByPrimaryKey(orderId.getOrderId());
//        cskaoyanmallOrder.setOrderStatus((short) 401);
        cskaoyanmallOrder.setUpdateTime(new Date());
        cskaoyanmallOrder.setDeleted(true);
        orderMapper.updateByPrimaryKeySelective(cskaoyanmallOrder);

        OrderGoodsExample cskaoyanmallOrderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = cskaoyanmallOrderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(cskaoyanmallOrder.getId());
        List<OrderGoods> cskaoyanmallOrderGoods = orderGoodsMapper.selectByExample(cskaoyanmallOrderGoodsExample);
        for (OrderGoods cskaoyanmallOrderGood : cskaoyanmallOrderGoods) {
            cskaoyanmallOrderGood.setUpdateTime(new Date());
            cskaoyanmallOrderGood.setDeleted(true);
            orderGoodsMapper.updateByPrimaryKey(cskaoyanmallOrderGood);
        }
    }

    @Override
    public void confirmOrder(WxOrderCancelBo orderId) {
        //订单待评价数量
        Order cskaoyanmallOrder = orderMapper.selectByPrimaryKey(orderId.getOrderId());
        cskaoyanmallOrder.setOrderStatus((short) 401);
        cskaoyanmallOrder.setUpdateTime(new Date());
        orderMapper.updateByPrimaryKeySelective(cskaoyanmallOrder);

        OrderGoodsExample cskaoyanmallOrderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = cskaoyanmallOrderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(cskaoyanmallOrder.getId());
        List<OrderGoods> cskaoyanmallOrderGoods = orderGoodsMapper.selectByExample(cskaoyanmallOrderGoodsExample);
        for (OrderGoods cskaoyanmallOrderGood : cskaoyanmallOrderGoods) {
            cskaoyanmallOrderGood.setUpdateTime(new Date());
            orderGoodsMapper.updateByPrimaryKey(cskaoyanmallOrderGood);
        }
    }

    @Override
    public OrderGoods getOrderGoods(Integer orderId, Integer goodsId) {
        OrderGoodsExample cskaoyanmallOrderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = cskaoyanmallOrderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        criteria.andGoodsIdEqualTo(goodsId);
        return orderGoodsMapper.selectByExample(cskaoyanmallOrderGoodsExample).get(0);
}


    private void checkEndTime(Integer userId, OrderExample cskaoyanmallOrderExample) {
        //查看endtime时间是否到时间
        OrderExample cskaoyanmallOrderExample1 = new OrderExample();
        OrderExample.Criteria criteria1 = cskaoyanmallOrderExample.createCriteria();
        criteria1.andUserIdEqualTo(userId);
        criteria1.andOrderStatusEqualTo(101);
        criteria1.andDeletedEqualTo(false);
        List<Order> cskaoyanmallOrders = orderMapper.selectByExample(cskaoyanmallOrderExample1);
        for (Order cskaoyanmallOrder : cskaoyanmallOrders) {
            if (cskaoyanmallOrder.getEndTime() != null && cskaoyanmallOrder.getEndTime().getTime() <= new Date().getTime()) {
//                cskaoyanmallOrder.setDeleted(true);
                cskaoyanmallOrder.setOrderStatus((short) 103);
                orderMapper.updateByPrimaryKey(cskaoyanmallOrder);
//                //将order_product表中的内容设为true
//                CskaoyanmallOrderGoodsExample cskaoyanmallOrderGoodsExample = new CskaoyanmallOrderGoodsExample();
//                CskaoyanmallOrderGoodsExample.Criteria criteria2 = cskaoyanmallOrderGoodsExample.createCriteria();
//                criteria2.andOrderIdEqualTo(cskaoyanmallOrder.getId());
//                criteria2.andDeletedEqualTo(false);
//                List<CskaoyanmallOrderGoods> cskaoyanmallOrderGoods = orderGoodsMapper.selectByExample(cskaoyanmallOrderGoodsExample);
//                for (CskaoyanmallOrderGoods cskaoyanmallOrderGood : cskaoyanmallOrderGoods) {
//                    cskaoyanmallOrder.setDeleted(true);
//                    orderGoodsMapper.updateByPrimaryKey(cskaoyanmallOrderGood);
//                }
                //cancel后，把库存还回去
                OrderGoodsExample cskaoyanmallOrderGoodsExample = new OrderGoodsExample();
                OrderGoodsExample.Criteria criteria = cskaoyanmallOrderGoodsExample.createCriteria();
                criteria.andOrderIdEqualTo(cskaoyanmallOrder.getId());
                List<OrderGoods> cskaoyanmallOrderGoods = orderGoodsMapper.selectByExample(cskaoyanmallOrderGoodsExample);
                for (OrderGoods cskaoyanmallOrderGood : cskaoyanmallOrderGoods) {
                    //CskaoyanmallGoodsProduct cskaoyanmallGoodsProduct =
                    ProductOfGoods cskaoyanmallGoodsProduct = goodsProductMapper.selectByPrimaryKey(cskaoyanmallOrderGood.getProductId());
                    cskaoyanmallGoodsProduct.setNumber(cskaoyanmallGoodsProduct.getNumber() + cskaoyanmallOrderGood.getNumber());
//                 cskaoyanmallOrderGood.setNumber();
                    goodsProductMapper.updateByPrimaryKey(cskaoyanmallGoodsProduct);
                }
            }
        }
    }


    private void setOrderInfo(OrderSubmitBo orderInfo, Order cskaoyanmallOrder, Integer userId, Address cskaoyanmallAddress) {
        cskaoyanmallOrder.setId(null);
        cskaoyanmallOrder.setUserId(userId);
        cskaoyanmallOrder.setOrderSn(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        cskaoyanmallOrder.setOrderStatus((short) 101);
        cskaoyanmallOrder.setConsignee(cskaoyanmallAddress.getName());
        cskaoyanmallOrder.setMobile(cskaoyanmallAddress.getMobile());
        cskaoyanmallOrder.setAddress(cskaoyanmallAddress.getAddress());
        cskaoyanmallOrder.setMessage(orderInfo.getMessage());
        cskaoyanmallOrder.setAddTime(new Date());
        cskaoyanmallOrder.setUpdateTime(new Date());
        cskaoyanmallOrder.setDeleted(false);
        cskaoyanmallOrder.setEndTime(new Date(new Date().getTime() + (mallConfig.getOrderUnpaid() * 60 * 1000)));//1小时后订单删除
        if (orderInfo.getCouponId() != 0 && orderInfo.getCouponId() != -1) {
            CouponUser cskaoyanmallCouponUser = couponUserMapper.selectByPrimaryKey(orderInfo.getCouponId());
            Coupon cskaoyanmallCoupon = couponMapper.selectByPrimaryKey(cskaoyanmallCouponUser.getCouponId());

            cskaoyanmallOrder.setCouponPrice(cskaoyanmallCoupon.getDiscount());//暂时定为折扣额度
            cskaoyanmallOrder.setOrderPrice(cskaoyanmallOrder.getGoodsPrice().add(cskaoyanmallOrder.getFreightPrice()).subtract(cskaoyanmallOrder.getCouponPrice()));
            cskaoyanmallOrder.setActualPrice(cskaoyanmallOrder.getOrderPrice());
            orderMapper.insertSelective(cskaoyanmallOrder);//插入到order数据库中
//                    int i = 1/0;
            //使用完优惠卷后，要修改优惠卷的信息
            cskaoyanmallCouponUser.setStatus((short) 1);
            cskaoyanmallCouponUser.setUsedTime(new Date());
            cskaoyanmallCouponUser.setOrderId(cskaoyanmallOrder.getId());
            cskaoyanmallCouponUser.setUpdateTime(new Date());

            couponUserMapper.updateByPrimaryKeySelective(cskaoyanmallCouponUser);
        } else {
            cskaoyanmallOrder.setOrderPrice(cskaoyanmallOrder.getGoodsPrice().add(cskaoyanmallOrder.getFreightPrice()));
            cskaoyanmallOrder.setActualPrice(cskaoyanmallOrder.getOrderPrice());
            cskaoyanmallOrder.setCouponPrice(BigDecimal.valueOf(1.0));
            cskaoyanmallOrder.setIntegralPrice(BigDecimal.valueOf(0.0));
            cskaoyanmallOrder.setGrouponPrice(BigDecimal.valueOf(0.0));
            orderMapper.insertSelective(cskaoyanmallOrder);//插入到order数据库中
        }
    }



    private Integer setOrderGoods(Order cskaoyanmallOrder, Cart cskaoyanmallCart) {
        OrderGoods cskaoyanmallOrderGoods = new OrderGoods();

        cskaoyanmallOrderGoods.setId(null);
        cskaoyanmallOrderGoods.setOrderId(cskaoyanmallOrder.getId());
        cskaoyanmallOrderGoods.setGoodsId(cskaoyanmallCart.getGoodsId());
        cskaoyanmallOrderGoods.setGoodsName(cskaoyanmallCart.getGoodsName());
        cskaoyanmallOrderGoods.setGoodsSn(cskaoyanmallCart.getGoodsId().toString());
        cskaoyanmallOrderGoods.setProductId(cskaoyanmallCart.getProductId());
        cskaoyanmallOrderGoods.setNumber(cskaoyanmallCart.getNumber());
        cskaoyanmallOrderGoods.setPrice(cskaoyanmallCart.getPrice());
        cskaoyanmallOrderGoods.setSpecifications(cskaoyanmallCart.getSpecifications());
        cskaoyanmallOrderGoods.setPicUrl(cskaoyanmallCart.getPicUrl());
        cskaoyanmallOrderGoods.setComment(0);
        cskaoyanmallOrderGoods.setAddTime(new Date());
        cskaoyanmallOrderGoods.setUpdateTime(new Date());
        cskaoyanmallOrderGoods.setDeleted(false);

        orderGoodsMapper.insertSelective(cskaoyanmallOrderGoods);//插入order_goods表

        //插入完成后修改购物车信息
        cskaoyanmallCart.setDeleted(true);
        cartMapper.updateByPrimaryKeySelective(cskaoyanmallCart);

        //插入完成后修改货物数量信息
        ProductOfGoods cskaoyanmallGoodsProduct = goodsProductMapper.selectByPrimaryKey(cskaoyanmallCart.getProductId());
        cskaoyanmallGoodsProduct.setNumber(cskaoyanmallGoodsProduct.getNumber() - cskaoyanmallCart.getNumber());
        if (cskaoyanmallGoodsProduct.getNumber() < 0) {
            return -1;
        }
        goodsProductMapper.updateByPrimaryKey(cskaoyanmallGoodsProduct);
        return 0;
    }





}
